package com.huayue.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.entity.RoomDeposit;
import com.huayue.service.RoomDepositService;
import com.huayue.util.DateHelper;
import com.huayue.util.PageHelper;

/**
 * 押金
 */
@Controller
@RequestMapping("/roomDeposit")
public class RoomDepositController {

    @Autowired
    RoomDepositService svc;

    BaseController bc = new BaseController();

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, RoomDeposit roomDeposit, String dateRange) {
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }
        return bc.findPage(svc, page, roomDeposit, roomDeposit, begin, end);
    }

    @RequestMapping("/returnById")
    public ModelAndView returnDepositById(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        int status = svc.returnDepositById(Integer.valueOf(id));
        if (status > 0) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "退还成功");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "退还失败");
        }
        return new ModelAndView(view);
    }
}
